package com.qtummatrix.dao.impl;

import com.qtummatrix.dao.CustomerNewDao;
import com.qtummatrix.entity.CustomerInfoVO;
import com.qtummatrix.entity.CustomerinfoEntity;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
/**
 * create by Gongshiyong  2019-11-21 19:39
 */

@Repository
public class CustomerNewDaoImpl implements CustomerNewDao {

	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;
	
	private Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	@Override
	public List<CustomerInfoVO> customerName(int id) {
		String sql= "select c.id,customerName,gender,tel,UID,address,customerWork,customerType,discredit,litigation,monthlyIncome,bankname,pledgename,customerState from CustomerinfoEntity as c "
				+ "inner join PledgeEntity as p on c.goodsId=p.id "
				+ "inner join BankinfoEntity as b on c.bankId=b.id "
				+ "where c.id=?";
		SQLQuery query=getSession().createSQLQuery(sql);
		query.setInteger(0, id);
		List<CustomerInfoVO> customerInfoVOs=new ArrayList<CustomerInfoVO>();
		List<Object[]> list=query.list();
		for(Object[] object:list) {
			CustomerInfoVO customerInfoVO=new CustomerInfoVO();
			customerInfoVO.setId((int)object[0]);
			customerInfoVO.setCustomerName(object[1].toString());
			customerInfoVO.setGender(object[2].toString());
			customerInfoVO.setTel(object[3].toString());
			customerInfoVO.setUID(object[4].toString());
			customerInfoVO.setAddress(object[5].toString());
			customerInfoVO.setCustomerWork(object[6].toString());
			customerInfoVO.setCustomerType(object[7].toString());
			customerInfoVO.setDiscredit(object[8].toString());
			customerInfoVO.setLitigation(object[9].toString());
			customerInfoVO.setMonthlyIncome((int)object[10]);
			customerInfoVO.setBankName(object[11].toString());
			customerInfoVO.setPledgeName(object[12].toString());
			customerInfoVO.setCustomerState(object[13].toString());
			customerInfoVOs.add(customerInfoVO);
		}
		return customerInfoVOs; 
		
	}

	@Override
	public void customerUpdate(int id,String customerName,String gender,String tel,String UID,String address,String customerWork,String customerType,String discredit,String litigation,int monthlyIncome,int bankId,int goodsId,String customerState,String createTime) {
		CustomerinfoEntity customerinfoEntity=new CustomerinfoEntity();
		customerinfoEntity.setId(id);
		customerinfoEntity.setCustomername(customerName);
		customerinfoEntity.setGender(gender);
		customerinfoEntity.setTel(tel);
		customerinfoEntity.setUid(UID);
		customerinfoEntity.setAddress(address);
		customerinfoEntity.setCustomerwork(customerWork);
		customerinfoEntity.setCustomertype(customerType);
		customerinfoEntity.setDiscredit(discredit);
		customerinfoEntity.setLitigation(litigation);
		customerinfoEntity.setMonthlyincome(monthlyIncome);
		customerinfoEntity.setBankid(bankId);
		customerinfoEntity.setGoodsid(goodsId);
		customerinfoEntity.setCustomerstate(customerState);
//		customerinfoEntity.setCreatetime(createTime);
		getSession().saveOrUpdate(customerinfoEntity);
	}
}
